package com.dm.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.entity.Product;
import com.dm.shop.entity.User;
import com.dm.shop.mapper.ProductMapper;
import com.dm.shop.mapper.UserMapper;
import com.dm.shop.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result addUser(User user) {
        boolean IsSave = this.save(user);
        if(IsSave!=true){
            System.out.println("注册失败！！！！！！！");
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            System.out.println("注册成功！！！！！！！");
            return ResultUtil.success(ResultEnum.SUCCESS);

        }
    }

    @Override
    public Result editUser(User user) {
         boolean IsUpdate =this.updateById(user);
        if(IsUpdate!=true){
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            return ResultUtil.success(ResultEnum.SUCCESS);

        }
    }

    @Override
    public Result getAllUserInfo(int page, int size) {
        IPage<User> userIPage =this.page(new Page<>(page,size),null);
        if(userIPage!=null){
            return ResultUtil.success(userIPage);
        }else {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result delUser(@Param("id") int id) {
        System.out.println("要删除的用户id："+id);
        boolean IsRemove =this.removeById(id);
        if(IsRemove!=true){
            return ResultUtil.error(ResultEnum.FAIL);
        }else {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result getUserByNickName(String nickname) {
        List<User> userList=userMapper.getUserByNickName(nickname);
        if(userList!=null){
            return ResultUtil.success(userList);
        }else {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

}
